//
//  ChangePasswordViewController.m
//  QinrenManagers
//
//  Created by easaa on 15/8/6.
//  Copyright (c) 2015年 easaa. All rights reserved.
//

#import "ChangePasswordViewController.h"

#import "NewPasswordCell.h"

@interface ChangePasswordViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITableView * changeTableView;

/**管家旧密码*/
@property (nonatomic, strong) NSString * old_password;
/**管家新密码*/
@property (nonatomic, strong) NSString * last_Password;

@property (nonatomic, strong) NSArray  * titleArry;

@end
//changeCell
@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UIButton *changeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [changeButton setTitle:@"修改" forState:UIControlStateNormal];
    [changeButton addTarget:self action:@selector(changeClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *changeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:changeButton];
    [self.navigationItem setRightBarButtonItem:changeButtonItem];
    

    [self creatData];
    [self creatTableView];
    
}




- (void)creatData
{
    
    
    
    _titleArry = [NSArray array];
    
    _titleArry = @[@"原密码",@"新密码",@"确认新密码"];
    
}

- (void)changeClick {
    UITextField *newPwdTF;
    UITextField *confirmPwdTF;
    for (int i = 0; i < 3; i ++) {
        NewPasswordCell *cell = (NewPasswordCell *)[self.changeTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        if (i == 0) {
            _old_password = cell.contentsTextField.text;
        }else if (i == 1) {
            newPwdTF = cell.contentsTextField;
            _last_Password = cell.contentsTextField.text;
        }else if (i == 2) {
            confirmPwdTF = cell.contentsTextField;
        }
    }
    if (![newPwdTF.text isEqualToString:confirmPwdTF.text]) {
        [SVProgressHUD showSuccessWithStatus:@"两次输入的新密码不一致"];
        return;
    }
    
    if (newPwdTF.text.length <= 0)
    {
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:@"密码不能为空"];
        return;
    }

    
    [self getVersionData];

}

- (void)getVersionData {
    
    User *user = [User sharedUser];
    
    EAHttpAPIClient *manager = [EAHttpAPIClient manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *parameters = @{@"employee_id":user.employee_id,@"old_password":self.old_password,@"new_password":self.last_Password};
    NSLog(@"%@",parameters);
    [manager GetWithParameters:parameters method:@"update.manager.password" success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *resposeString ;
        if ([responseObject isKindOfClass:[NSData class]]) {
            resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        }else
            resposeString = responseObject;
        
        NSString *jsonString = [DataParser parseString:resposeString];
        id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
        NSLog(@"修改密码的dictonary :%@",[dictionary description]);
        
        
        if ([[dictionary objectForKey:@"total"]intValue] == 1)
        {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
        }
        else if ([[dictionary objectForKey:@"total"]intValue] == 0)
        {
            [SVProgressHUD showSuccessWithStatus:@"修改失败"];
        }
        else if ([[dictionary objectForKey:@"total"]intValue] == 2)
        {
            [SVProgressHUD showSuccessWithStatus:@"旧密码不正确"];
        }
        else
        {
            NSString *error = [dictionary objectForKey:@"error"];
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:error];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
    }];
    
}


- (void)creatTableView {
    
    _changeTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) style:UITableViewStylePlain];
    _changeTableView.backgroundColor = [UIColor clearColor];
    _changeTableView.delegate = self;
    _changeTableView.dataSource = self;
    _changeTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
    UINib * nib = [UINib nibWithNibName:@"NewPasswordCell" bundle:nil];
    [_changeTableView registerNib:nib forCellReuseIdentifier:@"changeCell"];
    [self.view addSubview:_changeTableView];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.titleArry count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"changeCell";
    
    NewPasswordCell *cell = (NewPasswordCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[NewPasswordCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.titleLabel.text = _titleArry[indexPath.row];
    cell.contentsTextField.tag = 100+indexPath.row;
    
    
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}


@end
